#define _CRT_SECURE_NO_WARNINGS 1

#include"listnode.h"

LTNode* BuyNewNode(LTDataType x)
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	newnode->next = NULL;
	newnode->prev = NULL;
	newnode->data = x;
	return newnode;
}


LTNode* LTInit()
{
	LTNode* phead = BuyNewNode(0);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

void LTPrint(LTNode* phead)
{
	LTNode* cur = phead->next;
	printf("<=head=>");
	while (cur != phead)
	{
		printf("%d<=>", cur->data);
		cur = cur->next;
	}
	printf("\n");

}


void LTPushFront(LTNode* phead, LTDataType x)
{
	//LTInsert(phead->next, x);
	LTNode* newnode = BuyNewNode(x);
	newnode->next = phead->next;
	newnode->prev = phead;
	phead->next->prev = newnode;
	phead->next = newnode;
}

void LTPushBack(LTNode* phead, LTDataType x)
{
	//LTInsert(phead, x);
	LTNode* newnode = BuyNewNode(x);
	newnode->prev = phead->prev;
	phead->prev->next = newnode;
	newnode->next = phead;
	phead->prev = newnode;
}
void LTPopFront(LTNode* phead)
{
	LTNode* tmp = phead->next;
	tmp->next->prev = phead;
	phead->next = tmp->next;
	free(tmp);
}

void LTPopBack(LTNode* phead)
{
	LTNode* tmp = phead->prev;
	tmp->prev->next = phead;
	phead->prev = tmp->prev;
	free(tmp);
}

void LTInsert(LTNode* pos, LTDataType x)
{
	assert(pos);
	LTNode* newnode = BuyNewNode(x);
	newnode->prev = pos->prev;
	pos->prev->next = newnode;
	newnode->next = pos;
	pos->prev = newnode;
}

void LTErase(LTNode* pos)
{
	assert(pos);
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
	pos = NULL;

}

void LTDestroy(LTNode* phead)
{
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		LTNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
}